<template>
  <XtxDialog :title="`${ form.id ? '修改': '添加' }收货地址`" v-model="dialogVisible">
    <div class="address-edit">
      <!-- 表单 -->
     <div class="xtx-form-item">
          <div class="label">收货人：</div>
          <div class="field">
            <input v-model="form.receiver" class="input" placeholder="请输入收货人" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">手机号：</div>
          <div class="field">
            <input v-model="form.contact" class="input" placeholder="请输入手机号" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地区：</div>
          <div class="field">
            <XtxCity :fullLocation="form.fullLocation" @change="changeCty" placeholder="请选择所在地区" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">详细地址：</div>
          <div class="field">
            <input v-model="form.address" class="input" placeholder="请输入详细地址" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">邮政编码：</div>
          <div class="field">
            <input  v-model="form.postalCode"  class="input" placeholder="请输入邮政编码" />
          </div>
        </div>
        <div class="xtx-form-item">
          <div class="label">地址标签：</div>
          <div class="field">
            <input  v-model="form.addressTags"  class="input" placeholder="请输入地址标签，逗号分隔" />
          </div>
        </div>
    </div>
    <template v-slot:footer>
      <XtxButton type="gray" style="margin-right:20px" @click="dialogVisible=false">取消</XtxButton>
      <XtxButton type="primary" @click="OkBtn">确认</XtxButton>
    </template>
  </XtxDialog>
</template>
<script>
import { inject, reactive, ref } from 'vue'
import { addAddress, editAddress } from '@/api/order'
import Message from '@/components/library/Message'
export default {
  name: 'AddressEdit',
  setup (props, { emit }) {
    const dialogVisible = ref(false)
    // 打开函数
    const open = (val) => {
      // 判断是编辑还是新增
      if (val) {
        // 编辑
        for (const k in val) {
          form[k] = val[k]
        }
      } else {
        // 情况2：添加 - 清空空表单
        form.receiver = ''
        form.contact = ''
        form.address = ''
        form.postalCode = ''
        form.addressTags = ''
        form.isDefault = 1
      }
      dialogVisible.value = true
    }
    const form = reactive({
      receiver: '', // 收货人姓名
      contact: '', // 联系方式
      address: '', // 详细地址
      postalCode: '', // 邮政编码
      addressTags: '', // 地址标签,以英文逗号分割
      provinceCode: '', // 所在省份编码
      cityCode: '', // 所在城市编码
      countyCode: '', // 所在区/县编码
      isDefault: 1, // 是否为默认，0为是，1为否
      // 编辑地址需要的2个参数
      id: '',
      fullLocation: ''

    })
    // 城市选择组件传过来的数据
    const changeCty = (val) => {
      form.provinceCode = val.provinceCode
      form.cityCode = val.cityCode
      form.countyCode = val.countyCode
    }
    const getAddressList = inject('getAddressList')
    // 点击确定
    const OkBtn = async () => {
      if (form.id) {
        // 1-----点击编辑地址
        await editAddress(form)
        // 告诉父组件更新页面
        emit('upSite', form)
      } else {
        // 2-----点击新增地址
        await addAddress(form)
        // 调用爷爷组件的函数，刷新页面
        getAddressList()
      }
      // 💥我们这里省略了表单校验的代码...
      // 成功提示
      Message({ type: 'success', text: `${form.id ? '编辑' : '添加'}地址成功` })
      // 关闭窗口
      dialogVisible.value = false
    }
    return { dialogVisible, open, form, changeCty, OkBtn }
  }
}
</script>
<style scoped lang="less">
.xtx-dialog {
  :deep(.wrapper){
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
